package com.pi.pro.xcmg.am.was.web.cust.controller;


import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.led.common.LedWasXcmgExchangeConstants;
import com.pi.pro.xcmg.am.was.fac.service.LedCustomWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.base.LocationLineDto;
import com.pi.wms.entity.TblLedMessage;
import com.pi.wms.entity.TwmBasLocation;
import com.pi.wms.fac.LocationResolver;
import com.pi.wms.service.LedMessageService;
import com.pi.wms.service.LocationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Controller
@RequestMapping("/master/location/ca")
public class CustApplyLocationController extends BaseCrudController<TwmBasLocation, Long> {

	@Autowired
	private LocationService service;

	@Autowired
	private LocationResolver locationResolver;

	@Autowired
	private LedMessageService ledMessageService;

	@Autowired
	private LedCustomWorkService ledCustomWorkService;

	private int retrytimes=2;
	@Override
	protected ICrudService<TwmBasLocation, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "location";
	}

	@PostMapping("showledtext")
	public String showLedText(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {
		model.addAttribute("params", getParams(request));
		String msg="";
		boolean failed=false;
		LocationLineDto loc=null;
		/**
		if ((ids==null) || (ids.length==0)){
            return this.redirect("");
		}
         **/
		Long divisionid= XcmgAmConstants.DIVISION_WH101_ID;
        List<LocationLineDto> locs=new ArrayList<>();
        TblLedMessage led=null;
        if ((ids!=null)&&(ids.length>0)) {
            for (Long id : ids) {
                loc = this.locationResolver.lookup(id);
                if (loc == null) {
                    continue;
                }
                if (WmsConstants.LOCATION_TYPE_BIN.equalsIgnoreCase(loc.getLocationType())) {
                    continue;
                }
                locs.add(loc);
            }
        }
        if ((ids==null) || (ids.length==0)) {
            for (String s1:XcmgAmConstants.wh101LedLocations.keySet()) {
                loc=this.locationResolver.lookup(divisionid, s1);
                if (loc == null) {
                    continue;
                }
                if (WmsConstants.LOCATION_TYPE_BIN.equalsIgnoreCase(loc.getLocationType())) {
                    continue;
                }
                locs.add(loc);
            }
        }
        if ((locs!=null) && (locs.size()>0)) {
            for (int i=0; i<this.retrytimes; i++){
                for (LocationLineDto d:locs) {
                    if (d == null) {
                        continue;
                    }
                    try {
                        led = this.ledMessageService.findByCode(d.getDivisionId(), d.getLocationCode());
                        if (led == null) {
                            continue;
                        }
                        if (StringUtils.isNullOrBlank(led.getDf1())) {
                            continue;
                        }
                        if (LedWasXcmgExchangeConstants.LED_WORK_TYPE_PUTAWAY_STATION.equalsIgnoreCase(led.getType())) {
                            msg = this.ledCustomWorkService.showPutawayStationDefText(d.getDivisionId(), d.getLocationCode());
                        }
                        if (LedWasXcmgExchangeConstants.LED_WORK_TYPE_PICK_STATION.equalsIgnoreCase(led.getType())) {
                            msg = this.ledCustomWorkService.showPickStationDefText(d.getDivisionId(), d.getLocationCode());
                        }
                    } catch (Exception e) {
                        failed = true;
                        log.error("发送失败", e);
                        addErrorMessage(redirectAttributes, "发送失败");
                    }
                }
                try{
                    Thread.sleep(3011);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        if (!StringUtils.isNullOrBlank(msg)){
            addErrorMessage(redirectAttributes, msg);
            failed=true;
        }
        if (!failed){
            addSuccessMessage(redirectAttributes, "发送成功");
        }
		return this.redirect("");
	}

    protected String redirect(String relativeUrl) {
        if(!com.pi.common.utils.StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return "redirect:" + this.referUrl() + relativeUrl;
    }

    private String referUrl(){
        return "/master/location/";
    }

}
